import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.function.IntConsumer;

/**
 * @author hewei
 * @version 1.0
 * @description: TODO
 * @date 2022/11/1 21:37
 */

public class ZeroEvenOdd4 {

    private int n;
    private BlockingQueue<Integer> queue1;
    private BlockingQueue<Integer> queue2;
    private BlockingQueue<Integer> queue;

    public ZeroEvenOdd4(int n) {
        this.n = n;
        queue1 = new LinkedBlockingDeque<>();
        queue2 = new LinkedBlockingDeque<>();
        queue = new LinkedBlockingDeque<>();
        queue.add(1);
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero(IntConsumer printNumber) throws InterruptedException {
        for (int i = 0; i < n; i++) {
            queue.take();
            printNumber.accept(0);
            if (i % 2 == 0) {
                queue1.add(1);
            } else {
                queue2.add(1);
            }
        }
    }

    public void even(IntConsumer printNumber) throws InterruptedException {
        for (int i = 2; i <= n; i += 2) {
            queue2.take();
            printNumber.accept(i);
            queue.add(1);
        }
    }

    public void odd(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i += 2) {
            queue1.take();
            printNumber.accept(i);
            queue.add(1);
        }
    }
}
